package game;

import javax.sound.sampled.AudioInputStream;
import javax.sound.sampled.AudioSystem;
import javax.sound.sampled.Clip;
import java.io.File;

public class SoundManager {
    private volatile File file;
    //禁止重排序 懒汉--要我在给你（实例化时先不给对象） 饿汉--你要我就给
    public SoundManager(String file_url){
        this.file = new File(file_url);//文件
    }

    public void setFile(String file_url) {
        this.file = new File(file_url);
    }
    public void PlaySound(){
        Thread soundThread = new Thread(this::Sound);
        soundThread.start();
    }//创建新线程播放
    private void Sound(){

        try {
            AudioInputStream audioInputStream = AudioSystem.getAudioInputStream(file);
            //从文件中获取音频输入流。音频输入流被用来读取声音文件的内容。
            Clip clip = AudioSystem.getClip();
            clip.open(audioInputStream);
            clip.start();
            //睡眠时间以毫秒计算 但返回的音频长度是微妙

        } catch (Exception e){
            e.printStackTrace();
        }

    }


}
